package com.buss.article.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.jweb.core.bean.PageMode;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.article.trans.form.ArticleSearchForm;
import com.buss.article.trans.vo.ArticledetailVO;
import com.buss.auth.transfer.vo.authen.AuthUserVO;
import com.buss.common.entity.HxArticle;
import com.buss.common.service.SysServiceI;

@Controller
@RequestMapping("/articledetail")
public class ArticleDetailController {

	@Autowired
	private SysServiceI sysService;
	
	@RequestMapping("/list.do")
	@ResponseBody
	public ReplyDataMode articleList(ArticleSearchForm searchForm, HttpServletRequest request) {
		ReplyDataMode dataMode = new ReplyDataMode();

//		searchForm.setStatus(4+"");
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();

		addPublicQuery(criteriaQuery);
		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);
		//		
		List<HxArticle> articles = this.sysService.list(criteriaQuery);
		
		// 封装返回数据
		List<ArticledetailVO> articleVOs = new ArrayList<>();
		ArticledetailVO articleVO = null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for(HxArticle ha : articles) {
			articleVO = new ArticledetailVO();
			articleVO.copyEntity(ha);
			
			if(ha.getCreateTime() != null) {
				articleVO.setCreateTime(dateFormat.format(ha.getCreateTime()));
			}
			
			if(ha.getUpdateTime() != null) {
				articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime()));
			}
			articleVOs.add(articleVO);
		}
		
		dataMode.setData(articleVOs);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);
		
		return dataMode;
	}
	
	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ReplyDataMode articlePageList(ArticleSearchForm searchForm, HttpServletRequest request) {
		ReplyDataMode dataMode = new ReplyDataMode();
		CriteriaQuery criteriaQuery = searchForm.getCriteriaQuery();
		
		Map<String, Object> orderMap = new HashMap<>();
		orderMap.put("createTime", "desc");
		orderMap.put("updateTime", "desc");
		criteriaQuery.setOrder(orderMap);
		
		int pageNo = Integer.parseInt(searchForm.getPageNo());
		int pageSize = Integer.parseInt(searchForm.getPageSize());
		
		PageMode mode = this.sysService.list4page(criteriaQuery, pageNo, pageSize);
		List<HxArticle> articles = mode.getResult();
		
		// 封装返回数据
		List<ArticledetailVO> articleVOs = new ArrayList<>();
		ArticledetailVO articleVO = null;
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for(HxArticle ha : articles) {
			articleVO = new ArticledetailVO();
			articleVO.copyEntity(ha);
			
			/*if(ha.getCreateTime() != null) {
				articleVO.setCreateTime(dateFormat.format(ha.getCreateTime()));
			}
			
			if(ha.getUpdateTime() != null) {
				articleVO.setUpdateTime(dateFormat.format(ha.getUpdateTime()));
			}*/
			articleVOs.add(articleVO);
		}
		
		dataMode.setData(articleVOs);
		dataMode.setStatusCode("200");
		dataMode.setSuccess(true);
		
		return dataMode;
	}
	
	
	/**
	 * id查询
	 * @param searchForm
	 * @param request
	 * @return
	 */
	@RequestMapping({ "/byIdContent.do" })
	@ResponseBody
	public ReplyDataMode byIdContent(HttpServletRequest request)
			throws ServletException, IOException {
		ReplyDataMode j = new ReplyDataMode();
		List<HxArticle> entityList = null;
		
		String id = request.getParameter("id");
		if(id!=null&&id!=""){			
			entityList = sysService.findByProperty(HxArticle.class, "id", Long.parseLong(id));
		}
		
		List<ArticledetailVO> articleVOs = new ArrayList<>();
		ArticledetailVO articleVO = null;
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		for(HxArticle e : entityList){
			articleVO = new ArticledetailVO();
			articleVO.copyEntity(e);
			/*if(e.getCreateTime() != null) {
				articleVO.setCreateTime(dateFormat.format(e.getCreateTime()));
			}
			
			if(e.getUpdateTime() != null) {
				articleVO.setUpdateTime(dateFormat.format(e.getUpdateTime()));
			}*/
			articleVOs.add(articleVO);
		}
		
		j.setData(articleVOs);
		j.setStatusCode("200");
		j.setSuccess(true);
		
		return j;
	}

	private CriteriaQuery addPublicQuery(CriteriaQuery criteriaQuery) {
		Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.isAuthenticated()){
			AuthUserVO user = (AuthUserVO) currentUser.getSession().getAttribute("userInfo");
			int level = Integer.parseInt(user.getLevelclass());
			criteriaQuery.ge("levelclass", level);
			criteriaQuery.eq("status", 1);
			criteriaQuery.add();
		} else {
//			criteriaQuery.ge("levelclass", level);
			criteriaQuery.eq("status", 1);
			criteriaQuery.add();
		}

		 return criteriaQuery;
	}
	

	@RequestMapping({ "/test.do" })
	@ResponseBody
	public void test(HttpServletRequest request)
	{
		
		Subject currentUser = SecurityUtils.getSubject();
		AuthUserVO user = (AuthUserVO) currentUser.getSession().getAttribute("userInfo");
		String level = user.getLevelclass(); 
		System.out.println(level);
	}
	
}
